#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"

void STInit(ST* plist)
{
	assert(plist);
	plist->a = NULL;
	plist->top = 0;
	plist->capacity = 0;
}

void STDestroy(ST* plist)
{
	assert(plist);
	free(plist->a);
	plist->a = NULL;
	plist->top = plist->capacity = 0;
}

void STPush(ST* plist, STDataType x)
{
	assert(plist);
	if (plist->top == plist->capacity)
	{
		int newCapacity = plist->capacity == 0 ? 4 : plist->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(plist->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		plist->a = tmp;
		plist->capacity = newCapacity;
	}
	plist->a[plist->top] = x;
	plist->top++;
}

bool STEmpty(ST* plist)
{
	assert(plist);
	return plist->top == 0;
}

void STPop(ST* plist)
{
	assert(plist);
	assert(!STEmpty(plist));
	plist->top--;
}

STDataType STTop(ST* plist)
{
	assert(plist);
	assert(!STEmpty(plist));
	return plist->a[plist->top - 1];
}

int STSize(ST* plist)
{
	assert(plist);
	return plist->top;
}